package rpg.resource
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	/**
	 * 加载swf
	 * @author hyy
	 *
	 */
	public class SWFResource extends Resource
	{
		public var loader : Loader;
		public var appDomain : ApplicationDomain;

		public function SWFResource(url : String , returnFun : Function)
		{
			super(url , returnFun);
		}

		override public function load() : void
		{
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS , onProgressEvent);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE , onDownLoadComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR , onDownloadError);
			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR , onDownloadSecurityError);
			loader.load(request);
		}

		override protected function onDownLoadComplete(evt : Event) : void
		{
			if(loader && loader.contentLoaderInfo)
				appDomain = loader.contentLoaderInfo.applicationDomain;
			super.onDownLoadComplete(evt);
		}

		public function getAssetClass(name : String) : Class
		{
			if(appDomain == null)
				throw new Error("not initialized");
			if(appDomain.hasDefinition(name))
				return appDomain.getDefinition(name) as Class;
			else
				return null;
		}

		override protected function stop() : void
		{
			try
			{
				if(loader)
				{
					loader.close();
				}
			}
			catch(e : Error)
			{
			}
			super.stop();
		}

		override public function destory() : void
		{
			appDomain = null;
//			if(loader)
//				loader.unloadAndStop();
			super.destory();
		}

	}
}